package study;

import java.util.*;

public class CollcetionSort {
    public void sortList(){
        List<Integer> listSort = new ArrayList<Integer>();
        System.out.println("请输入集合长度");
        Scanner input = new Scanner(System.in);
        int number = input.nextInt();
        Random random = new Random();
        Integer m;
        for (int i=0;i<number;i++){
            do{
                m=random.nextInt(100);
            }while(listSort.contains(m));
            listSort.add(m);
            System.out.println("成功添加整数"+ (i+1)+"："+m);
        }
        System.out.println("---------排序前----------");
        for (Integer listnum:listSort) {
            System.out.println(listnum);
        }
        System.out.println("----------排序后---------");
        Collections.sort(listSort);
        for (Integer listnum:listSort) {
            System.out.println(listnum);
        }
    }

    public void stringList(){
        List<String> listString = new ArrayList<String>();
        System.out.println("请输入集合长度");
        Random random = new Random();
        String str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ012345678";
        Scanner input=new Scanner(System.in);
        int n=input.nextInt();

        for (int i=0;i<n;i++){
            int a=random.nextInt(10);
            StringBuilder sb = new StringBuilder();
            if (a>0){
               do{
                   for(int j=0;j<a;j++){
                       sb.append(str.charAt(random.nextInt(str.length())));
                   }
               }while(listString.contains(sb));
               listString.add(sb.toString());
            }
        }
        System.out.println("-------排序前---------");
        for(String a:listString){
            System.out.println("字符串："+a);
        }
        System.out.println("-------排序后---------");
        Collections.sort(listString);
        for(String a:listString){
            System.out.println("字符串："+a);
        }
    }

    public static void main(String[] args){
        CollcetionSort collcetionSort= new CollcetionSort();
        collcetionSort.sortList();
        collcetionSort.stringList();
        System.exit(0);
    }
}

